// pages/createPatient/index.js
const app = getApp()
const api = require("./../../config/api")
const request = require("./../../utils/request")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isEdit: 0,
		isEditIdCard: false,
		patientInfo: {
			age: '',
			allergy: "",
			birthday: "",
			contact: "",
			contactRelationType: "",
			contactTel: "",
			familyHistory: "",
			idCard: "",
			nation: "",
			pastHistory: "",
			patientName: "",
			personalHistory: "",
			postnatalFactors: null,
			prenatalFactors: null,
			productionFactors: null,
			relation: "",
			sex: ''
		},
		today: '',
		showModalStatus: false,

		twoWord: [
			'汉族', '回族', '满族', '土族', '藏族', '羌族', '彝族', '傣族',
			'佤族', '怒族', '苗族', '侗族', '水族', '壮族', '瑶族', '京族',
			'黎族', '畲族', '白族'
		],
		threeWord: [
			'蒙古族', '朝鲜族', '赫哲族', '东乡族', '撒拉族', '保安族',
			'裕固族', '门巴族', '珞巴族', '哈尼族', '僳僳族', '拉祜族',
			'纳西族', '景颇族', '布朗族', '阿昌族', '普米族', '德昂族',
			'独龙族', '基诺族', '布依族', '仡佬族', '仫佬族', '毛南族',
			'土家族', '高山族', '锡伯族'
		],
		fourWord: [
			'鄂温克族', '达斡尔族', '鄂伦春族', '塔吉克族', '俄罗斯族',
			'维吾尔族', '哈萨克族', '塔塔尔族'
		],
		fiveWord: [
			'柯尔克孜族', '乌孜别克族'
		]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var timestamp = Date.parse(new Date());
		var date = new Date(timestamp);
		//获取年份  
		var Y = date.getFullYear();
		//获取月份  
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
		//获取当日日期 
		var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
		this.setData({
			today: Y + '-' + M + '-' + D
		})

		if (options.patientInfo) {
			const patientInfo = JSON.parse(options.patientInfo)
			this.getPatientDetial(patientInfo.id)
		}
	},
	getPatientDetial: async function(id) {
		const res = await request.get(api.patientDetail + '/' + id)
		if (res.httpStatus === 200) {
			switch (res.data.contactRelationType) {
				case 1:
					res.data.contactRelationType = '配偶'
					break;
				case 2:
					res.data.contacRelationType = '子'
					break;
				case 3:
					res.data.contactRelationType = '女'
					break;
				case 4:
					res.data.contactRelationType = '孙子、孙女、或外孙子、外孙女'
					break;
				case 5:
					res.data.contactRelationType = '父母'
					break;
				case 6:
					res.data.contactRelationType = '祖父母'
					break;
				case 7:
					res.data.contactRelationType = '外祖父母'
					break;
				case 8:
					res.data.contactRelationType = '兄、弟、姐、妹'
					break;
				case 9:
					res.data.contactRelationType = '其他'
					break;
				default:
			}
			res.data.birthday = res.data.birthday.substring(0, 10)
			this.setData({
				patientInfo: res.data,
				isEdit: 1,
				isEditIdCard: true
			})
		}
	},
	bindDateChange(e) {
		let patientInfo = this.data.patientInfo
		patientInfo.birthday = e.detail.value
		this.setData({
			patientInfo: patientInfo
		})
	},
	sexChange(e) {
		let patientInfo = this.data.patientInfo
		patientInfo.sex = Number(e.detail.value)
		this.setData({
			patientInfo: patientInfo
		})
	},
	idCart(e) {
		let patientInfo = this.data.patientInfo
		const idCard = e.detail.value
		const idCardNo = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		if (!idCardNo.test(idCard)) {
			return
		}
		// 截取出生日期
		let birthdayno, birthdaytemp, sexno, sex
		if (idCard.length === 18) {
			birthdayno = idCard.substring(6, 14)
			sexno = idCard.substring(16, 17)
		} else if (idCard.length === 15) {
			birthdaytemp = idCard.substring(6, 12)
			sexno = idCard.substring(14, 15)
			birthdayno = "19" + birthdaytemp
		}
		let birthday = birthdayno.substring(0, 4) + "-" + birthdayno.substring(4, 6) + "-" + birthdayno
			.substring(6, 8)
		// 获取性别
		var tempid = sexno % 2;
		if (tempid == 0) {
			sex = 2
		} else {
			sex = 1
		}
		patientInfo.birthday = birthday
		patientInfo.sex = sex
		patientInfo.idCard = idCard
		this.setData({
			patientInfo: patientInfo
		})
	},
	nation() {
		this.setData({
			showModalStatus: true
		})
	},
	chooseNation(val) {
		let patientInfo = this.data.patientInfo
		patientInfo.nation = val.currentTarget.dataset.item
		this.setData({
			patientInfo: patientInfo
		})
	},
	patientName(val) {
		let patientInfo = this.data.patientInfo
		patientInfo.patientName = val.detail.value
		this.setData({
			patientInfo: patientInfo
		})
	},
	savaBtnTap() {
		this.setData({
			showModalStatus: false
		})
	},
	next: async function() {
		let patientInfo = this.data.patientInfo
		if (!patientInfo.patientName) {
			wx.showToast({
				title: '请填写患者姓名',
				icon: "none",
			})
			return
		}
		if (!patientInfo.idCard) {
			wx.showToast({
				title: '请填写患者身份证号',
				icon: "none",
			})
			return
		}
		if (!patientInfo.nation) {
			wx.showToast({
				title: '请选择民族',
				icon: "none",
			})
			return
		}
		if (this.data.isEdit === 0) {
			const token = wx.getStorageSync('token')
			wx.request({
				url: 'https://zhiliaopingtai.qingshanyun.cn/applet/patient/select-by-id-card/' + patientInfo
					.idCard,
				header: {
					Authorization: 'Bearer ' + token
				},
				success: (res => {
					const data = res.data
					if (data.httpStatus === 200) {
						wx.showModal({
							content: '该患者已经注册，是否前往绑定患者？',
							cancelText: "取消", //默认是“取消”
							confirmText: "去绑定", //默认是“确定”
							success: function(modelData) {
								if (modelData.cancel) {
									//点击取消,默认隐藏弹框
								} else {
									//点击确定
									const patientDate = JSON.stringify(data
										.data)
									wx.navigateTo({
										url: '../bindPatient/index?patientDate=' +
											patientDate
									})
								}
							},
							fail: function(data) {}, //接口调用失败的回调函数
							complete: function(data) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
						})
					} else {
						app.globalData.changePatientInfo = patientInfo
						const item = {
							isEdit: this.data.isEdit
						}
						const urlData = JSON.stringify(item)
						wx.navigateTo({
							url: './contact?data=' + urlData
						})
					}
				}),
				fail: (res => {})
			})
		} else {
			app.globalData.changePatientInfo = patientInfo
			const item = {
				isEdit: this.data.isEdit
			}
			const urlData = JSON.stringify(item)
			wx.navigateTo({
				url: './contact?data=' + urlData
			})
		}

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
